package source.events.action;

import source.model.type.ActionResult;
import source.model.DataHandler;
import source.model.Player;
import source.model.Unit;
import source.model.UnitMission;

public class CommitMission implements Action{

	private int playerID;
	private UnitMission um;
	public CommitMission (int playerID, UnitMission um) {
		this.playerID = playerID;
		this.um = um;
	}
	
	public ActionResult execute(DataHandler dh)
	{
		Player p = dh.getPlayer(playerID);
		p.addUnitMission(um);
		Unit u = (Unit)dh.objectQuery(um.getUnitID());
		u.setMissionFlag(true);
		
		ExecuteMission em = new ExecuteMission(playerID, um);
		em.execute(dh);

		if(!um.isValid())
			p.removeUnitMission(um);

		NextUnit nu = new NextUnit(playerID);
		nu.execute(dh);
		
		return ActionResult.SUCCESS;
	}
}
